package com.example;

import com.ctc.wstx.api.WstxOutputProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;

import javax.xml.stream.XMLOutputFactory;

public class XmlUtil {
    public final static XmlMapper xmlMapper;

    static {
        xmlMapper = new XmlMapper();
        xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
//        xmlMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
        XmlFactory factory = xmlMapper.getFactory();
//        xmlMapper.enable(SerializationFeature.CLOSE_CLOSEABLE);
//        SerializationConfig serializationConfig = xmlMapper.getSerializationConfig();
//        serializationConfig.with(SerializationFeature.CLOSE_CLOSEABLE);
        XMLOutputFactory xmlOutputFactory = factory.getXMLOutputFactory();
        xmlOutputFactory.setProperty(WstxOutputProperties.P_USE_DOUBLE_QUOTES_IN_XML_DECL, true);
        xmlOutputFactory.setProperty(WstxOutputProperties.P_OUTPUT_FIX_CONTENT, true);
        xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
        //字段为null，自动忽略，不再序列化
        xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //XML标签名:使用骆驼命名的属性名，
        xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
    }
}
